home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Watch Volume Mount / launcher.cc next >
Text File  |  1994-02-10  |  4KB  |  128 lines

  1. /*
  2.  ***********************************************************************
  3.  *
  4.  *        This is a simple program that, given a file specification, launches
  5.  * an application-creator and has it handle the file. The net result is
  6.  * exactly the same as if the user had double-clicked on the file.
  7.  *
  8.  * Synopsis
  9.  *        void open_selection(const FSSpec& file_spec)
  10.  * Where the 'file_spec' tells the file specification (volume refno,
  11.  * directory ID and the sile name) of the file to "double-click" on.
  12.  *
  13.  * The present program achieves the magic by sending an 'Open Selection'
  14.  * event to the Finder. It is significantly based on the FinderEvents
  15.  * stack by Jon Pugh and Apple Computer, Inc. (© 1991-92 Apple Computer, Inc.)
  16.  *
  17.  * OpenSelection event requires two parameters:
  18.  *    - alias of the Folder the file resides in
  19.  *    - list containing an alias of the file(s) to "double-click" on
  20.  *
  21.  *
  22.  ***********************************************************************
  23.  */
  24.  
  25. /* MacHeaders Included */
  26. #include "myenv.h"
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <AppleEvents.h>
  30. #include <Aliases.h>
  31.  
  32.  
  33.                             // Create an 'open selection' event to be sent to Finder
  34. static AppleEvent create_event_for_finder(void)
  35. {
  36.     const AEEventClass     kAEFinderEvents = 'FNDR';        // Finder event
  37.     const AEEventID     kOpenSelection = 'sope';        // OpenSelection event
  38.     const OSType         kFinderSignature = 'MACS';        // Our destination
  39.     AppleEvent             the_event;
  40.     AEAddressDesc         finder_id;
  41.     
  42.     do_well( AECreateDesc(typeApplSignature,(void *)&kFinderSignature,sizeof(kFinderSignature),
  43.                        &finder_id) );
  44.  
  45.     do_well( AECreateAppleEvent(kAEFinderEvents,kOpenSelection,&finder_id,
  46.                             kAutoGenerateReturnID,
  47.                             kAnyTransactionID,
  48.                             &the_event) );
  49.  
  50.     do_well( AEDisposeDesc(&finder_id) );                    // the_event is created, finder_id can
  51.                                                               // be disposed of now
  52.       
  53.     return the_event;
  54. }
  55.  
  56.                                     // Add the alias of file or path name to the event
  57.                                     // Only the path fieled is considered
  58. static void add_path_name(AppleEvent * the_event_ptr, const FSSpec& file_spec)
  59. {
  60.     AliasHandle alias;
  61.     
  62.     FSSpec dir_spec;
  63.     do_well( FSMakeFSSpec(file_spec.vRefNum,file_spec.parID,"\p",&dir_spec) );
  64.     do_well( NewAliasMinimal(&dir_spec,&alias) );
  65.     HLock((char **)alias);
  66.     do_well( AEPutParamPtr(the_event_ptr, keyDirectObject, typeAlias, StripAddress(*alias),
  67.                          (**alias).aliasSize) );
  68.  
  69.     HUnlock((char **)alias);
  70.     DisposHandle((char **)alias);
  71. }
  72.  
  73.  
  74.                                     // Create a "selection" item in the event from the
  75.                                     // full file name specified 
  76. static void add_selection(AppleEvent * the_event_ptr, const FSSpec& file_spec)
  77. {
  78.     AliasHandle alias;
  79.     AEDescList selection_list;
  80.     
  81.     do_well( NewAliasMinimal(&file_spec,&alias) );
  82.  
  83.     do_well( AECreateList(nil, 0, FALSE, &selection_list) );
  84.     HLock((char **)alias);
  85.     do_well( AEPutPtr(&selection_list, 1, typeAlias, StripAddress(*alias), 
  86.                    (**alias).aliasSize) );
  87.     HUnlock((char **)alias);
  88.     DisposHandle((char **)alias);
  89.  
  90.     do_well( AEPutParamDesc(the_event_ptr, 'fsel', &selection_list) );
  91.     do_well( AEDeleteItem(&selection_list, 1) );
  92.     assert( AEDisposeDesc(&selection_list) == noErr );
  93. }
  94.  
  95.  
  96. //-------------------------------------------------------------------------
  97. //                            Routing module
  98. //
  99.  
  100. void open_selection(const FSSpec& file_spec)
  101.     char path_name[255];                // Path to the file, ends in :
  102.     char * p;
  103.     
  104.     AppleEvent the_event, reply;
  105.     
  106.  
  107. #if 0   
  108.                                         // Getting just a path name from the full
  109.                                         // file name
  110.     assure(strlen(full_path_name) < sizeof(path_name),
  111.            "Full path name is too big");
  112.     if( (p=strrchr(full_path_name,':')) == (char *)0 )
  113.       path_name[0] = '\0';                            // Path name is empty
  114.     else
  115.       strncpy(path_name,full_path_name,p-full_path_name+1);
  116. #endif
  117.               
  118.     the_event = create_event_for_finder();
  119.     add_path_name(&the_event,file_spec);
  120.     add_selection(&the_event,file_spec);
  121.     
  122.     do_well( AESend(&the_event, &reply, kAENoReply+kAENeverInteract, 
  123.                  kAENormalPriority, kAEDefaultTimeout, nil, nil) );
  124.  
  125.     do_well( AEDisposeDesc(&the_event) );
  126.     do_well( AEDisposeDesc(&reply) );
  127. }